package com.example.huangds.myapplication;

import com.alibaba.fastjson.JSON;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;

/**
 * @Description: Http请求工具类
 * @author Huangds
 * @date 2018/5/8 0:33
 */
public class HttpUtil {
    /**
     * @Description: 执行HttpPost方法，并返回结果String
     * @author Huangds
     * @date 2018/5/8 0:34
     */
    public static String excuteHttpPost(final String Url, Map<String, String> paramMap) throws InterruptedException, ExecutionException {
        String result = null;
        if (paramMap != null && paramMap.size() > 0) {
            final List<NameValuePair> pairList = new ArrayList<NameValuePair>();
            for (Entry<String, String> paramEntry : paramMap.entrySet()) {
                NameValuePair pair = new BasicNameValuePair(paramEntry.getKey(), paramEntry.getValue());
                pairList.add(pair);
            }

            ExecutorService threadPool = Executors.newSingleThreadExecutor();
            Future<String> future = threadPool.submit(new Callable<String>() {

                @Override
                public String call() throws Exception {
                    HttpPost httpPost = new HttpPost(Url);
                    HttpEntity requestHttpEntity = new UrlEncodedFormEntity(pairList);
                    httpPost.setEntity(requestHttpEntity);
                    HttpClient httpClient = new DefaultHttpClient();
                    HttpResponse response = httpClient.execute(httpPost);
                    String callResult = showResponseResult(response);
                    return callResult;
                }

            });
            return future.get();
        }
        return result;
    }

    /**
     * @Description: 读取执行请求返回结果
     * @author Huangds
     * @date 2018/5/8 0:32
     */
    public static String showResponseResult(HttpResponse response) {
        if (null == response) {
            return null;
        }

        String result = "";
        HttpEntity httpEntity = response.getEntity();
        try {
            InputStream inputStream = httpEntity.getContent();
            BufferedReader reader = new BufferedReader(new InputStreamReader(
                    inputStream));
            String line = "";
            while (null != (line = reader.readLine())) {
                result += line;

            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        return result;
    }

    /**
     * @Description: 执行HttpPost方法，并解析为List<Map<String, Object>>
     * @author Huangds
     * @date 2018/5/8 0:32
     */
    public static List<Map<String, Object>> excutePostJsonList(String Url, Map<String, String> paramMap) throws InterruptedException, ExecutionException {
        String result = excuteHttpPost(Url, paramMap);
        List resultJsonList = JSON.parseArray(result);
        return resultJsonList;
    }

    /**
     * @Description: 执行HttpPost方法，并解析为Map<String, Object>
     * @author Huangds
     * @date 2018/5/8 0:31
     */
    public static Map<String, Object> excutePostJson(String Url, Map<String, String> paramMap) throws InterruptedException, ExecutionException {
        String result = excuteHttpPost(Url, paramMap);
        Map<String, Object> resultMap = JSON.parseObject(result);
        return resultMap;
    }

}
